import os

# 命令模板
#
# 其中，
#   - src 入口文件，这里为 `rt-thread/include/rtthread.h`
#   - clang_arg 为提供给编译器 clang 的一些 flag
#
# `--use-core` 使用 `core` 库，而不是 std 
# ·--verbose` 提供更多的提示信息
CMD = "bindgen {src} -o bindings.rs --use-core --verbose -- {clang_arg}"


src = "rt-thread/include/rtthread.h"


clang_arg = [
    "-march=armv7-a",
    "-marm",
    "-msoft-float",
    "-DHAVE_CCONFIG_H",
    "-DRT_USING_NEWLIB",
    "-I.",
    "-Iapplications",
    "-Idrivers",
    "-Irt-thread/include",
    "-Irt-thread/libcpu/arm/common",
    "-Irt-thread/libcpu/arm/cortex-a",
    "-Irt-thread/components/cplusplus",
    "-Irt-thread/components/dfs/include",
    "-Irt-thread/components/dfs/filesystems/devfs",
    "-Irt-thread/components/dfs/filesystems/elmfat",
    "-Irt-thread/components/dfs/filesystems/ramfs",
    "-Irt-thread/components/dfs/filesystems/romfs",
    "-Irt-thread/components/drivers/include",
    "-Irt-thread/components/drivers/spi",
    "-Irt-thread/components/drivers/spi/sfud/inc",
    "-Irt-thread/components/finsh",
    "-Irt-thread/components/libc/aio",
    "-Irt-thread/components/libc/compilers/newlib",
    "-Irt-thread/components/libc/mmap",
    "-Irt-thread/components/libc/pthreads",
    "-Irt-thread/components/libc/signal",
    "-Irt-thread/components/libc/termios",
    "-Irt-thread/components/libc/time",
    "-Irt-thread/components/lwp",
    "-Irt-thread/components/net/lwip-2.0.2/src",
    "-Irt-thread/components/net/lwip-2.0.2/src/include",
    "-Irt-thread/components/net/lwip-2.0.2/src/include/ipv4",
    "-Irt-thread/components/net/lwip-2.0.2/src/arch/include",
    "-Irt-thread/components/net/lwip-2.0.2/src/include/netif",
    "-Irt-thread/components/net/netdev/include",
    "-Irt-thread/components/net/sal_socket/include",
    "-Irt-thread/components/net/sal_socket/include/socket",
    "-Irt-thread/components/net/sal_socket/impl",
    "-Irt-thread/components/net/sal_socket/include/dfs_net",
    "-Irt-thread/components/net/sal_socket/include/socket/sys_socket",

    # 需要增加 target ，才不会报链接出错 
    "--target=armv7a-none-eabi",

    # 其还会报没有 error: 'signal.h' 没有找到此文件，所以要把 arm-none-eabi-gcc 的库给标识出来 
    # 这里我使用了 RT-thread Studio 里的 gcc 
    "-ID:/ProgramFiles/RT-ThreadStudio/repo/Extract/ToolChain_Support_Packages/ARM/GNU_Tools_for_ARM_Embedded_Processors/5.4.1/arm-none-eabi/include"
]



reply = os.popen(CMD.format(src=src, clang_arg=" ".join(clang_arg)))
print(reply.read())